//
//  BaseTabBarController.m
//  Project
//
//  Created by 冯东辉 on 2018/9/17.
//  Copyright © 2018年 FengDongHui. All rights reserved.
//

#import "BaseTabBarController.h"
#import "HomeVC.h"
#import "CollectVC.h"
#import "MyVC.h"

@interface BaseTabBarController () <UITabBarControllerDelegate>

@end

@implementation BaseTabBarController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.tabBarController.selectedIndex = 0;
    [self setupChildControllers];
    
    self.delegate = self;
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

/**
 创建子控制器
 */
- (void)setupChildControllers
{
    [self setupChildNavigationControllerWithClass:[HomeVC class]
                                  tabBarImageName:@"tabbar_home"
                          rootViewControllerClass:[HomeVC class]
                          rootViewControllerTitle:@"概况"];
    
    [self setupChildNavigationControllerWithClass:[CollectVC class]
                                  tabBarImageName:@"tabbar_collect"
                          rootViewControllerClass:[CollectVC class]
                          rootViewControllerTitle:@"收藏"];
    
    [self setupChildNavigationControllerWithClass:[MyVC class]
                                  tabBarImageName:@"tabbar_my"
                          rootViewControllerClass:[MyVC class]
                          rootViewControllerTitle:@"我"];
}

/**
 添加子控制器的封装方法
 */
- (void)setupChildNavigationControllerWithClass:(Class)class
                                tabBarImageName:(NSString *)name
                        rootViewControllerClass:(Class)rootViewControllerClass
                        rootViewControllerTitle:(NSString *)title {
    
    BaseViewController *rootVC = [[rootViewControllerClass alloc] init];
    
    UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:rootVC];
    
    [self controller:navVc Title:nil tabBarItemImage:name tabBarItemSelectedImage:[NSString stringWithFormat:@"%@_Sel", name]];
    
    [self addChildViewController:navVc];
}

/**
 * 抽取成一个方法
 * 传入控制器、标题、正常状态下图片、选中状态下图片
 * 直接调用这个方法就可以了
 */
- (void)controller:(UIViewController *)controller Title:(NSString *)title tabBarItemImage:(NSString *)image tabBarItemSelectedImage:(NSString *)selectedImage
{
    
    controller.tabBarItem.image = [UIImage imageNamed:image];
    // 设置 tabbarItem 选中状态的图片(不被系统默认渲染,显示图像原始颜色)
    UIImage *imageHome = [UIImage imageNamed:selectedImage];
    imageHome = [imageHome imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [controller.tabBarItem setSelectedImage:imageHome];
    
    controller.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
    
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
}

@end
